<!--  -->
<template>
  <div class="root">
    <product-add
      name="cx"
      :age="18"
      :calculate="calculate"
      :tableData="tableData"
      :datas="datas"
      @re="testValue"
    >
      <!-- @给谁绑定的就找谁 -->
    </product-add>

    <anthoer @gc="generalConduction"></anthoer>
    <cartoon></cartoon>
 <input type="button" value="请求home消息" @click="testCommon">
  </div>
</template>

<script>
import productAdd from "@/views/product/productAdd";
import anthoer from "@/views/product/anthoer";
import cartoon from "@/views/product/cartoon";
import axios from "axios";
export default {
  name: "Category",
  components: { productAdd, anthoer, cartoon },
  created() {
    this.reduceTest();
    console.log("父组件created进来了");
  },
  mounted() {
    console.log("父组件进来了mounted");
  },

  data() {
    return {
      tableData: [
        {
          date: "2016-05-02",
          name: "王小猫",
          address:
            "新疆维吾尔自治区昌吉回族自治州木垒哈萨克自治县大南沟乌孜别克族乡(32字)和新疆维吾尔自治区伊犁哈萨克自治州察布查尔锡伯自治县米粮泉回族乡",
        },
        {
          date: "2016-05-04",
          name: "王小狗",
          address: "上海市普陀区金沙江路 1517 弄",
        },
        {
          date: "2016-05-01",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1519 弄",
        },
        {
          date: "2016-05-03",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1516 弄",
        },
      ],
      datas: [],
    };
  },
  methods: {
    testCommon() {
      axios.get("http://localhost:8080/api/common/home").then(
        response => {
          console.log("请求成功了", response.data);
        },
        error => {
          console.log("请求失败了", error.message);
        }
      );
    },

    testValue(data) {
      console.log("testValue被调用了");
      console.log("得到的值为", data);
    },

    calculate(data) {
      console.log("我被点了");
      console.log(data);
    },
    reduceTest() {
      // const x = this.tableData.reduce((pre, current) => {
      //   return current.name === "王小虎" ? pre + 1 : pre;
      // }, 0);
      // console.log("最终的值为" + x);
      return this.tableData.reduce(
        (pre, curent) => (curent.name === "王小虎" ? pre + 1 : pre),
        0
      );
    },
    generalConduction(data) {
      console.log("父组件进来了", data);
      this.datas = data;
    },
  },
};
</script>
<style scoped>
</style>